<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col :span="4">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table :data="roleList" border stripe>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-row v-for="(item,index) in scope.row.children" :key="item.id"
                  :class="['vcenter','bdBtm',index===0?'bdTop':''] ">
            <el-col :span="5">
              <el-tag closable @close="removeRightById(scope.row,item.id)">{{item.authName}}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <el-col :span="19">
              <el-row v-for="(itemSub,indexSub) in item.children" :key="itemSub.id"
                      :class="['vcenter','bdBtm',indexSub!==0?'bdTop':'']">
                <el-col :span="6">
                  <el-tag type="success" closable @close="removeRightById(scope.row,itemSub.id)">{{itemSub.authName}}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="18">
                  <el-col :span="24">
                    <el-tag v-for="(itemSun) in itemSub.children" :key="itemSun.id" type="warning" closable
                            @close="removeRightById(scope.row,itemSun.id)">{{itemSun.authName}}
                    </el-tag>
                  </el-col>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>
          <el-tooltip effect="dark" content="分配角色" placement="top">
            <el-button type="warning" icon="el-icon-setting" size="mini"
                       @click="showSetRightDialog(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="分配权限"
      :visible.sync="showSetDialogVisible"
      width="50%"
      @close="setRightDialogClose()"
    >
      <el-tree :data="rightList" :props="treeProps" show-checkbox node-key="id" default-expand-all
               :default-checked-keys="checkedKeys" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="showSetDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotRight">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        roleList: [],
        rightList: [],
        showSetDialogVisible: false,
        treeProps: {
          label: 'authName',
          children: 'children'
        },
        checkedKeys: [],
        roleId: ''
      }
    },
    created () {
      this.getRoles()
    },
    methods: {
      async getRoles () {
        const { data: res } = await this.$http.get('roles')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.roleList = res.data
      },
      async removeRightById (role, rightId) {
        const conformResult = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (conformResult == 'cancel') {
          return this.$message.success('取消用户删除！')
        }
        if (conformResult == 'confirm') {
          const { data: delRes } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
          if (delRes.meta.status !== 200) {
            return this.$message.error(delRes.meta.msg)
          } else {
            this.$message.success('用户删除成功！')
            role.children = delRes.data
          }
        }
      },
      async showSetRightDialog (role) {
        this.roleId = role.id
        const { data: res } = await this.$http.get('rights/tree')

        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.rightList = res.data
        this.getLeafKeys(role, this.checkedKeys)
        this.showSetDialogVisible = true
      },
      setRightDialogClose () {
        this.checkedKeys = []
      },
      getLeafKeys (node, arr) {
        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach(item => this.getLeafKeys(item, arr))
      },
      //分配权限
      async allotRight () {
        //this.showSetDialogVisible = false
        const keys = [
          ...this.$refs.treeRef.getCheckedKeys(),
          ...this.$refs.treeRef.getHalfCheckedKeys()
        ]
        const idstr = keys.join(',')
        const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: idstr })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getRoles()
        this.showSetDialogVisible = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-tag {
    margin: 7px;
  }

  .bdTop {
    border-top: 1px solid #eee;
  }

  .bdBtm {
    border-bottom: 1px solid #eee;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }
</style>
